package struts.form;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;

public class Struts_xml {
	public static Map<String,XmlBean> struts_xml(String xmlPath) throws Exception{

		SAXBuilder builder = new SAXBuilder();
		Document document = builder.build(new File(xmlPath));
		Element root = document.getRootElement();
		Map<String,XmlBean> rmap= new HashMap<String,XmlBean>();
		
		Element action_mapping=root.getChild("action-mapping");
		List<Element> actions=action_mapping.getChildren();
		XmlBean xmlBean=new XmlBean();
		for (Element action:actions){
			String name=action.getAttributeValue("name");
			xmlBean.setBeanName(name);
			String path=action.getAttributeValue("path");
			xmlBean.setActionType(path);
			String type=action.getAttributeValue("type");
			xmlBean.setActionType(type);
			
			Element formbeans=root.getChild("formbeans");
			List<Element> beans=formbeans.getChildren();
			for (Element bean:beans){
				String beanName=bean.getAttributeValue("name");
				if(name.equals(beanName)){
					String class1=bean.getAttributeValue("class");
					xmlBean.setBeanClass(class1);
					break;
				}
			}
			
			List<Element> forwards=action.getChildren();
			Map<String,String> map=new HashMap<String,String>();
			for (Element forward:forwards){
				String forwardName=forward.getAttributeValue("name");
				String forwardValue=forward.getAttributeValue("value");
				map.put(forwardName, forwardValue);
			}
			xmlBean.setForward(map);
			rmap.put(path, xmlBean);
		}
		return rmap;
	}
}
